package org.jzs.mybaseapp.common.retorift;

import org.json.JSONException;
import org.json.JSONObject;
import org.jzs.mybaseapp.common.utils.AppLog;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;

/**
 * Created by Jzs on 2017/7/29 0029.
 * <p>
 * 状态拦截器，根据状态码来判断接口返回情况
 */

public class RspCheckInterceptor implements Interceptor {

    @Override
    public Response intercept(Chain chain) throws IOException {

        //添加全局的header
        Request original = chain.request();
        Request request = original.newBuilder()
                .header("isUser", "2")
                .method(original.method(), original.body())
                .build();

        //添加全局的接口返回状态码拦截
        Response response = chain.proceed(request);
        try {
            ResponseBody rspBody = response.body();
            JSONObject jsonObject = new JSONObject(InterceptorUtils.getRspData(rspBody));
            int code = jsonObject.getInt("code");
            AppLog.e("返回状态码code" + code);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return response;
    }
}